<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/admin.php';

/**
 * Контроллер просмотра и редактирования контента меню
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Admin_Manage_Contents extends Admin {
   
   /**
    * Идентификатор меню
    *
    * @access private
    * @var integer
    */
   var $_id_menu = 0;
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Admin_Manage_Contents
    */
   function Admin_Manage_Contents() {
      parent::Admin();
      $this->set_menu(4, 1);
      // Получаем идентификатор меню
      $this->load->model('m_table');
      $this->m_table->set_table('menus');
      if (false !== $this->input->get('menu')) {
         $this->_id_menu = (int) $this->input->get('menu');
         $this->m_table->add_restriction('id_menu', $this->_id_menu);
         $this->m_table->load_results();
         if (0 == $this->m_table->get_count()) {
            $this->_id_menu = 0;
         }
      }
      if (0 == $this->_id_menu) {
         $this->m_table->clean_restrictions();
         $this->m_table->add_sort('position');
         $this->m_table->set_limit(1);
         $this->m_table->load_results();
         if (0 < $this->m_table->get_count()) {
            $result = $this->m_table->next_result();
            $this->_id_menu = (int) $result['id_menu'];
         } else {
            $this->redirect('admin');
         }
      }
      $this->load->language('button');
   } // end Admin_Manage_Contents
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      $data = array();
      $this->load->helper('link');
      $this->load->model('m_select_contents');
      $this->m_select_contents->set_menu($this->_id_menu);
      $this->m_select_contents->initialize();
      $this->load->library('table_builder');
      $this->table_builder->set_model($this->m_select_contents);
      $this->table_builder->add_button(create_link($this->get_controller(), 'activate', array('menu' => $this->_id_menu)), $this->lang->line('activate_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'pause', array('menu' => $this->_id_menu)), $this->lang->line('pause_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'update_positions', array('menu' => $this->_id_menu)), $this->lang->line('update_positions_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'set_default', array('menu' => $this->_id_menu)), $this->lang->line('set_default_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'add', array('menu' => $this->_id_menu)), $this->lang->line('add_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'edit', array('menu' => $this->_id_menu)), $this->lang->line('edit_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'delete', array('menu' => $this->_id_menu)), $this->lang->line('delete_button'), $this->lang->line('contents_delete_confirmation'));
      $data['table'] = $this->table_builder->generate();
      $this->set_content($this->parser->parse('admin/manage_contents.html', $data, true));
      $this->display();
   } // end index
   
   /**
    * Метод активации записей
    *
    * @access public
    */
   function activate() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $this->load->model('m_content');
         foreach ($ids as $id) {
            $this->m_content->set_id($id);
            $this->m_content->activate();
         }
         $this->set_message($this->lang->line('activate_contents_succ'), 'info', true);
      }
      $this->redirect($this->get_controller(), 'index', array('menu' => $this->_id_menu));
   } // end activate
   
   /**
    * Метод приостановки записей
    *
    * @access public
    */
   function pause() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $this->load->model('m_content');
         foreach ($ids as $id) {
            $this->m_content->set_id($id);
            if ('no' == $this->m_content->get('is_default')) {
               $this->m_content->pause();
            }
         }
         $this->set_message($this->lang->line('pause_contents_succ'), 'info', true);
      }
      $this->redirect($this->get_controller(), 'index', array('menu' => $this->_id_menu));
   } // end pause
   
   /**
    * Метод удаления записей
    *
    * @access public
    */
   function delete() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $this->load->model('m_content');
         foreach ($ids as $id) {
            $this->m_content->set_id($id);
            if ('no' == $this->m_content->get('is_default')) {
               $this->m_content->delete();
            }
         }
         $this->set_message($this->lang->line('delete_contents_succ'), 'info', true);
      }
      $this->redirect($this->get_controller(), 'index', array('menu' => $this->_id_menu));
   } // end delete
   
   /**
    * Метод добавления записи
    * Редирект на нужный контроллер
    *
    * @access public
    */
   function add() {
      $this->redirect('admin_update_content', 'index', array('menu' => $this->_id_menu));
   } // end add
   
   /**
    * Метод изменения записи
    * Редирект на нужный контроллер
    *
    * @access public
    */
   function edit() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $id = array_shift($ids);
         $this->redirect('admin_update_content', '', array('menu' => $this->_id_menu, 'id' => $id));
      }
      $this->redirect($this->get_controller(), 'index', array('menu' => $this->_id_menu));
   } // end edit
   
   /**
    * Метод установки позиций
    *
    * @access public
    */
   function update_positions() {
      $positions = $this->input->post('positions');
      if (is_array($positions) && 0 < count($positions)) {
         $this->load->model('m_content');
         foreach ($positions as $id => $position) {
            $this->m_content->set_id($id);
            $this->m_content->set('position', $position);
            $this->m_content->update();
         }
         $this->set_message($this->lang->line('update_positions_contents_succ'), 'info', true);
      }
      $this->redirect($this->get_controller(), 'index', array('menu' => $this->_id_menu));
   } // end update_positions
   
   /**
    * Метод установки контента по умолчанию
    *
    * @access public
    */
   function set_default() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $id = array_shift($ids);
         $this->load->model('m_content');
         $this->m_content->set_id($id);
         $this->m_content->set_default();
         $this->set_message($this->lang->line('set_default_content_succ'), 'info', true);
      }
      $this->redirect($this->get_controller(), 'index', array('menu' => $this->_id_menu));
   } // end set_default
   
   /**
    * Метод получения HTML представления пользовательского меню
    *
    * @access protected
    * @return string
    */
   function _get_menu() {
      $output = parent::_get_menu();
      $output = str_replace('{%id_menu%}', $this->_id_menu, $output);
      return $output;
   } // end _get_menu
   
}
